#include"math.h"
#include"cv.h"
#include"highgui.h"
#include"stdio.h"
#include <iostream>

using namespace std;

#define CAMERA -1

//////////////////////DECLARATIONS////////////////////////////////
int i=0;
int j=0;
int k=0;/*initializing the iteraiton variables to be zero*/
int temp=0;		//if we use a temporary var
int dil_thresh = 1;	//value for dilution slider
int ero_thresh = 1;	//value for erosion slider

int heighthsv,widthhsv,stephsv,channelshsv;
int heightmono,widthmono,stepmono,channelsmono;
uchar *datahsv,*datamono;
int sthreshold=140;

double hlower=175,hupper=5;

CvCapture* capture;

IplImage* frame = 0;
IplImage *colimgbot = 0;
IplImage *monoimgbot = 0;

//////////////////SLIDER BAR FUNCTIONS////////////////////////////
void change_dil(int dummy_one){
	cvDilate( monoimgbot,monoimgbot,0,dil_thresh);
	cvShowImage("Monochrome Of Red Blob", monoimgbot);
}

void change_ero(int dummy_two){
	cvErode(monoimgbot,monoimgbot,0,ero_thresh);
	cvShowImage("Monochrome Of Red Blob", monoimgbot);
}
/////////////////////MAIN//////////////////////////////////////
int main(int argc, char** argv){
	
	cvNamedWindow( "Monochrome Of Red Blob", CV_WINDOW_AUTOSIZE );
	cvCreateTrackbar("Dilation", "Monochrome Of Red Blob", &dil_thresh, 20, change_dil); //create slider bar for hue and saturation
	cvCreateTrackbar("Erosion", "Monochrome Of Red Blob", &ero_thresh, 20, change_ero); //create slider bar for hue and saturation
	if(argc == 1){							//error-checking
		capture = cvCreateCameraCapture( CAMERA );		//pick any camera if multiple cameras
	}
	else{
		capture = cvCreateFileCapture(argv[1]);			//pick video file specified
	}
	
	while(1) {
		frame = cvQueryFrame( capture );			//break up the stream into images
		colimgbot = cvCreateImage( cvGetSize(frame), 8, 3 );
		monoimgbot = cvCreateImage( cvGetSize(frame), 8, 1 );
		
		heighthsv = colimgbot->height;
		widthhsv = colimgbot->width;
		stephsv =colimgbot->widthStep;
		channelshsv = colimgbot->nChannels;
		datahsv = (uchar *)colimgbot->imageData;
		//———————----------------

		heightmono = monoimgbot ->height;
		widthmono = monoimgbot->width;
		stepmono = monoimgbot->widthStep;
		channelsmono = monoimgbot->nChannels;
		datamono = (uchar *)monoimgbot->imageData;
		
		if (!frame){						//error-checking
			cout <<"Error :(\n" ;
			break;
		}
		
		cvCvtColor(frame,colimgbot,CV_RGB2HSV);
		
		for(i=0;i< (heighthsv);i++){
			for(j=0;j<(widthhsv);j++){
				if((datahsv[(i)*stephsv+j*channelshsv]<=hlower) && (datahsv[(i)*stephsv+j*channelshsv]>=hupper)){ 
					if((datahsv[(i)*stephsv+j*(channelshsv)+1])>sthreshold){
						datamono[i*stepmono+j*channelsmono]=255;
					}else datamono[i*stepmono+j*channelsmono]=0;
				}
				if(!(datamono[i*stepmono+j*channelsmono]==0 || datamono[i*stepmono+j*channelsmono]==255))
				datamono[i*stepmono+j*channelsmono]=0;
			}
		}
		//re-initialize slider bars
		change_dil(0);
		change_ero(0);	
		
		char c = cvWaitKey(33);					//20ms between each image/frame
		cvReleaseImage(&colimgbot);
		cvReleaseImage(&monoimgbot);
		if (c == 27) break;					//if user presses 'esc', loop breaks (program ends)
	}

}
